﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using System.Data;


namespace MainSystemForm.handler
{
    class SQLHelper : DbHelper
    {
        /// <summary>
        ///\查询操作
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public DataTable Selectinfo(string sql)
        {
            MySqlConnection conn = null;
            MySqlDataAdapter adapter = null;
            DataTable dt = null;
            try
            {
                conn = base.GetConn();
                adapter = new MySqlDataAdapter(sql, conn);
                dt = new DataTable();
                adapter.Fill(dt);
            }
            catch(Exception)
            {
                throw new Exception("数据库连接异常，请检查系统配置！");
            }
            finally
            {
                if (conn != null)
                {
                    conn.Close();
                }
            }

            return dt;
        }

        /// <summary>
        /// 增删改操作
        /// </summary>
        /// <param name="sql"></param>
        public void AddDelUpdate(string sql)
        {
            MySqlConnection conn = null;
            MySqlCommand cmd = null;

            try
            {
                conn = base.GetConn();
                conn.Open();
                cmd = new MySqlCommand(sql, conn);

                int result = cmd.ExecuteNonQuery();
                if (result == 0)
                {
                    throw new Exception("数据操作失败！");
                }
            }
            catch (Exception)
            {
                throw new Exception("数据库连接异常，请检查系统配置！");
            }
            finally
            {
                if (conn != null)
                {
                    conn.Close();
                }
            }
        }
    }
}
